﻿using System;
using System.Web.UI;
using FineUI;
using ZHDJ.WebPage;
using System.Collections.Generic;
using ZHDJ.Data.Models.JoinParty;
using ZHDJ.Data;

namespace ZHDJ.JoinParty.BLL
{
    public class PageBase : RolePageBase
    {
        #region 页面初始化

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            // 此用户是否有访问此页面的权限
            if (!CheckPowerView())
            {
                CheckPowerFailWithPage();
                return;
            }

            // 设置主题
            if (PageManager.Instance != null)
            {
                PageManager.Instance.Theme = (Theme)Enum.Parse(typeof(Theme), ConfigHelper.Theme, true);
            }

            UpdateOnlineUser(User.Identity.Name);

            // 设置页面标题
            Page.Title = ConfigHelper.Title;
        }

        #endregion
        /// <summary>
        /// 获取当前流程ID
        /// </summary>
        public string CurrentStandID
        {
            get
            {
                return Request.QueryString["StandID"] ?? string.Empty;
            }
        }

        /// <summary>
        /// 用户入党申请所属组织ID
        /// </summary>
        public string ApplyBranchID
        {
            get
            {
                return Request.QueryString["ApplyBranchID"] ?? string.Empty;
            }
        }

        /// <summary>
        /// 获取当前步聚ID
        /// </summary>
        public string CurrentStepID
        {
            get
            {
                if (string.IsNullOrEmpty(Request.QueryString["CurrentStepID"]))
                {
                    PartyJoinLinkExample mode = DB.PartyJoinLinkExamples.Find(this.CurrentStandID);
                    return mode.CurrentStepID;
                }
                else
                {
                    return Request.QueryString["CurrentStepID"] ?? string.Empty;
                }
            }
        }
        /// <summary>
        /// 是否只读页面（默认可读写）  1：只读
        /// </summary>
        public string ReadOnly
        {
            get
            {
                return Request.QueryString["ReadOnly"] ?? string.Empty;
            }
        }

        /// <summary>
        /// 是否入党流程管理员
        /// </summary>
        public bool IsJoinPartyAdmin
        {
            get
            {
                bool flag = false;
                foreach (string item in ModuleConfigs.JoinPartyManager)
                {
                    if (this.CurrentUser.UserNo.Equals(item))
                    {
                        flag = true;
                        break;
                    }
                }
                return flag;
            }
        }

        /// <summary>
        /// 是否前台调用
        /// </summary>
        public bool Stype
        {
            get
            {
                var type = "n";
                if (Microsoft.Security.Application.AntiXss.HtmlEncode(Request.QueryString["Stype"]) != null)
                {
                    type = Microsoft.Security.Application.AntiXss.HtmlEncode((Request.QueryString["Stype"].Trim()));
                }
                return type == "y";
            }
        }

        /// <summary>
        /// 获取申请ID
        /// </summary>
        public string AppllicationID
        {
            get
            {
                if (string.IsNullOrEmpty(Request.QueryString["AppllicationID"]))
                {
                    var oModel = DB.PartyJoinLinkExamples.Find(this.CurrentStandID);
                    if (oModel != null)
                    {
                      return  oModel.AppllicationID;
                    }

                    return "";
                }
                else
                {
                    return Request.QueryString["AppllicationID"];
                }
            }
        }
        #region 上传文件类型判断

        protected readonly static List<string> VALID_FILE_TYPES = new List<string> { "jpg", "bmp", "gif", "jpeg", "png" };

        protected static bool ValidateFileType(string fileName)
        {
            string fileType = String.Empty;
            int lastDotIndex = fileName.LastIndexOf(".");
            if (lastDotIndex >= 0)
            {
                fileType = fileName.Substring(lastDotIndex + 1).ToLower();
            }

            if (VALID_FILE_TYPES.Contains(fileType))
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        #endregion

        /// <summary>
        /// 是否为管理员
        /// </summary>
        public bool IsAdmin
        {
            get
            {
                return ModuleUtil.IsAdmin(this.CurrentUser.LoginID);
            }
        }

        /// <summary>
        /// 当前登陆用户组织ID
        /// </summary>
        public string CurrentUserBranchID
        {
            get
            {
                if (string.IsNullOrEmpty(Microsoft.Security.Application.AntiXss.HtmlEncode(Request.QueryString["BranchID"] ?? "")))
                {
                    return PartyOrg.QueryMemberBranchID(this.CurrentUser.LoginID ?? "");
                }
                else
                    return Microsoft.Security.Application.AntiXss.HtmlEncode(Request.QueryString["BranchID"].ToString());
            }
        }
    }
}
